<?php

use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Permission;

class AdminDataSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $map = [
            '文章'=>'文章',
            '设置'=>'设置',
            '主面板'=>'主面板',
            '运营'=>'运营',
        ];

        $menus = [
            [
                'name' => '主面板',
                'url' => '',
                'permission_name' => 'backend.dashboard',
                'children' => [
                    [
                        'name' => '仪表盘',
                        'permission_name' => 'backend.dashboard.index',
                    ],
                ],
            ],
            [
                'name' => '运营',
                'url' => '',
                'permission_name' => 'backend.operative',
                'children' => [
                    [
                        'name' => '会员列表',
                        'permission_name' => 'backend.member.index',
                    ],
                    [
                        'name' => '前台导航&友情链接',
                        'permission_name' => 'backend.link.index',
                    ],
                ],
            ],
            [
                'name' => '文章',
                'url' => '',
                'permission_name' => 'backend.article',
                'children' => [
                    [
                        'name' => '所有文章',
                        'permission_name' => 'backend.article.index',
                    ],
                    [
                        'name' => '写文章',
                        'permission_name' => 'backend.article.create',
                    ],
                    [
                        'name' => '文章评论',
                        'permission_name' => 'backend.comment.index',
                    ],
                    [
                        'name' => '分类',
                        'permission_name' => 'backend.category.index',
                    ],
                    [
                        'name' => 'Tag',
                        'permission_name' => 'backend.tag.index',
                    ],
                ],
            ],
            [
                'name' => '设置',
                'url' => '',
                'permission_name' => 'backend.setting',
                'children' => [
                    [
                        'name' => '管理员',
                        'permission_name' => 'backend.admin.index',
                    ],
                    [
                        'name' => '角色',
                        'permission_name' => 'backend.role.index',
                    ],
                    [
                        'name' => '权限',
                        'permission_name' => 'backend.permission.index',
                    ],
                    [
                        'name' => '后台菜单',
                        'permission_name' => 'backend.menu.index',
                    ],
                    [
                        'name' => '系统设置',
                        'permission_name' => 'backend.setting.index',
                    ],
                    [
                        'name' => '操作日志',
                        'permission_name' => 'backend.operation_log.index',
                    ],
                    [
                        'name' => '登陆日志',
                        'permission_name' => 'backend.login_log.index',
                    ],
                ],
            ],
        ];

        foreach ($menus as $index => $menu) {
            $data = [
                'name' => $menu['name'],
                'url' => $menu['permission_name'],
                'permission_name' => $menu['permission_name'],
                'order' => $index,
                'parent_id' => 0,
            ];
            $node = \App\Models\Menu::where('name', $data['name'])->where('url', $data['url'])->first();
            if (! $node) {
                $node = \App\Models\Menu::create($data);
            }

            foreach ($menu['children'] as $i => $item) {
                if (\App\Models\Menu::where('name', $item['name'])->where('permission_name', $item['permission_name'])->exists()) {
                    continue;
                }
                $data = $item;
                if (! isset($data['permission_name'])) {
                    $displayName = isset($map[$item['name']]) ? $map[$item['name']] : $item['name'];
                    $permission = Permission::where('display_name', $displayName)->first();
                    $data['permission_name'] = $permission ? $permission->name : '';
                }
                $data['order'] = $i;
                $data['url'] = $item['permission_name'];
                $data['parent_id'] = $node->id;
                \App\Models\Menu::create($data);
            }
        }
    }
}
